package com.cskaoyan.service.category;

import com.cskaoyan.bean.CommonData;
import com.cskaoyan.bean.bo.category.CategoryBo;
import com.cskaoyan.bean.po.ohter.MarketCategory;
import com.cskaoyan.bean.po.ohter.MarketCategoryExample;
import com.cskaoyan.bean.vo.category.CategoryListVo;
import com.cskaoyan.bean.vo.category.LableListVo;
import com.cskaoyan.mapper.MarketCategoryMapper;
import com.cskaoyan.util.common.BeanTransfer;
import com.cskaoyan.util.common.DateUtils;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * @author Zhang Tao
 * @version 1.0.0
 * @description
 * @date since 2022-07-20 14:02
 */
@Service
public class CategoryServiceImpl implements CategoryService {
    @Autowired
    MarketCategoryMapper categoryMapper;

    @Override
    public CommonData selectAll() {
        MarketCategoryExample example = new MarketCategoryExample();
        MarketCategoryExample.Criteria criteria = example.createCriteria();
        criteria.andDeletedEqualTo(false).andLevelEqualTo("L1");
        List<MarketCategory> marketCategories = categoryMapper.selectByExample(example);
        CategoryListVo categoryListVo1 = new CategoryListVo();
        List<CategoryListVo> listVos=new ArrayList<>();
        for (MarketCategory marketCategory : marketCategories) {
            Integer id = marketCategory.getId();
            try {
                CategoryListVo categoryListVo = new CategoryListVo();
                categoryListVo1 = (CategoryListVo) BeanTransfer.transfer(marketCategory, categoryListVo);
                MarketCategoryExample example1 = new MarketCategoryExample();
                MarketCategoryExample.Criteria criteria1 = example1.createCriteria();
                criteria1.andDeletedEqualTo(false).andLevelEqualTo("L2").
                        andPidEqualTo(id);
                List<MarketCategory> marketCategories1 = categoryMapper.selectByExample(example1);
                categoryListVo1.setChildren(marketCategories1);
                listVos.add(categoryListVo1);
            } catch (IllegalAccessException e) {
                e.printStackTrace();
            }

        }
        PageHelper.startPage(1, marketCategories.size());
        PageInfo<CategoryListVo> info = new PageInfo<>(listVos);
        return CommonData.data(info);
    }
    @Override
    public CommonData selectLable() {
        LableListVo lableListVo = new LableListVo();
        MarketCategoryExample example = new MarketCategoryExample();
        MarketCategoryExample.Criteria criteria = example.createCriteria();
        criteria.andDeletedEqualTo(false).andLevelEqualTo("L1");
        List<MarketCategory> marketCategories = categoryMapper.selectByExample(example);
        List<LableListVo> listVos=new ArrayList<>();
        for (MarketCategory marketCategory : marketCategories) {
            lableListVo.setValue(marketCategory.getId());
            lableListVo.setLable(marketCategory.getName());
            listVos.add(lableListVo);
        }
        PageHelper.startPage(1,marketCategories.size());
        PageInfo<LableListVo> info = new PageInfo<>(listVos);
        return CommonData.data(info);


    }
    @Override
    public MarketCategory createCategory(MarketCategory category) {
        category.setAddTime(DateUtils.time());
        category.setUpdateTime(DateUtils.time());
        category.setDeleted(false);
        categoryMapper.insertSelective(category);
        MarketCategory marketCategory = categoryMapper.selectByPrimaryKey(category.getId());
        return marketCategory;


    }

    @Override
    public void updateCategory(CategoryBo categoryBo) {
        MarketCategory marketCategory = new MarketCategory();
        MarketCategory marketCategory1=null;
        try {
            marketCategory1 = (MarketCategory) BeanTransfer.transfer(categoryBo,marketCategory);
            marketCategory1.setUpdateTime(DateUtils.time());
            categoryMapper.updateByPrimaryKeySelective(marketCategory1);
        } catch (IllegalAccessException e) {
            e.printStackTrace();
        }

    }

    @Override
    public void deleteCategory(CategoryListVo categoryListVo) {
        MarketCategory marketCategory = new MarketCategory();
        Integer id = categoryListVo.getId();
        marketCategory.setUpdateTime(DateUtils.time());
            marketCategory.setDeleted(true);
            marketCategory.setId(id);
            categoryMapper.updateByPrimaryKeySelective(marketCategory);
        MarketCategoryExample example = new MarketCategoryExample();
        MarketCategoryExample.Criteria criteria = example.createCriteria();
        criteria.andPidEqualTo(id).andDeletedEqualTo(false);
        List<MarketCategory> marketCategories = categoryMapper.selectByExample(example);
        for (MarketCategory category : marketCategories) {
            category.setDeleted(true);
            category.setUpdateTime(DateUtils.time());
            categoryMapper.updateByPrimaryKeySelective(category);
        }

    }


}
